//
//  MYGChatShareReportController.swift
//  MYGrow
//
//  Created by 小火科技 on 2024/3/4.
//

import UIKit
import MJRefresh

class MYGChatShareReportController: MYGBaseController {
    
    var sendAction: ((String) -> Void)?
    
    private var page = 1
    private var contents: [MYGMineReportModel] = []
    private var selectDatas: [MYGMineReportModel] = []
    // MARK: - life cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configUI()
       
    }
    // MARK: - UI
    private func configUI() {
        self.title = "分享报告"
        view.addSubview(tableView)
        view.addSubview(shareButton)
        view.addSubview(emptyView)
        
        tableView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(shareButton.snp.top)
        }
        tableView.mj_header?.beginRefreshing()
        shareButton.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(emptyView.snp.top)
            make.height.equalTo(49)
        }
        
        emptyView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(bottomSafeheight)
        }
    }
    // MARK: - lazy load
    private lazy var tableView: UITableView = {
        let tableV = UITableView()
        tableV.separatorStyle = .none
        tableV.tableHeaderView = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: 15))
        tableV.backgroundColor = .rgba(240, 240, 240)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.register(cellWithClass: MYGChatShareReportCell.self)
        tableV.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.page = 1
            self?.getList()
        })
        
        tableV.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {[weak self] in
            self?.page += 1
            self?.getList()
        })
        return tableV
    }()
    
    private lazy var shareButton: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = .rgba(162, 146, 254, 1)
        btn.titleLabel?.font = .medium(17)
        btn.setTitle("确认分享", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.addTarget(self, action: #selector(clickShareBtn), for: .touchUpInside)
        return btn
    }()
    
    private lazy var emptyView: UIView = {
        let view = UIView()
        view.backgroundColor = .rgba(240, 240, 240)
        return view
    }()
}

extension MYGChatShareReportController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        contents.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        1
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        110
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: MYGChatShareReportCell.self, for: indexPath)
        let model = contents[indexPath.section]
        cell.model = model
        cell.isSelected = !findCellIndex(index: indexPath.section)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model:MYGMineReportModel = contents[indexPath.section]
        let M = selectDatas.filter{ $0.id == model.id }
        if (M.isEmpty){
            self.selectDatas.append(contents[indexPath.section])
        }else {
            self.selectDatas.removeAll { $0.id == model.id }
        }
       
        tableView.reloadSections(IndexSet(integer: indexPath.section), with:.automatic)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        15
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView()
    }
    
}

extension MYGChatShareReportController {
    
    @objc
    func clickShareBtn(){
        //selectDatas
        self.sendAction?("这个是报告链接")
        navigationController?.popViewController(animated: true)
    }

    func findCellIndex(index:Int) -> Bool {
        let model:MYGMineReportModel = contents[index]
        return selectDatas.filter{ $0.id == model.id }.isEmpty
    }
    
    
    func getList() {
//        let patientId:String = "\(MYGUserManager.getUser()?.id ?? 0)"
//        MYGNetwork<MYGMineAPI, MYGPageModel<MYGMineReportModel>>.request(target:.reportList(patient_id: patientId)) { item in
//            MYGLoading.hide()
//            guard let item = item else {
//                return
//            }
//            self.tableView.mj_header?.endRefreshing()
//            if item.page.page <= 1 {
//                self.contents = item.list
//            } else {
//                self.contents.append(contentsOf: item.list)
//            }
//            
//            if item.page.total <= self.contents.count {
//                self.tableView.mj_footer?.endRefreshingWithNoMoreData()
//            } else {
//                self.tableView.mj_footer?.endRefreshing()
//            }
//            
//            self.tableView.reloadData()
//            
//            
//        } failed: { _, error in
//            MYGLoading.hide()
//            MMYGHUD.showToast(text: error)
//        }
    }
}
